package com.bestcem.xm.member.grpc.service;

import com.bestcem.xm.member.grpc.service.param.member.GetNameSequenceParam;
import com.bestcem.xm.member.grpc.v1.services.GetNameSequenceRequest;
import com.bestcem.xm.member.grpc.v1.services.SequenceServiceGrpc;
import com.bestcem.xm.member.service.SequenceService;
import net.devh.boot.grpc.server.service.GrpcService;

import javax.annotation.Resource;

/**
 * 提供默认名称序号相关rpc服务
 *
 * @author yf.wang <yf.wang@idiaoyan.com>
 * @version V1.0
 * @date 2021/7/13
 */
@GrpcService
public class SequenceGrpcService extends SequenceServiceGrpc.SequenceServiceImplBase {

    @Resource
    private SequenceService sequenceService;

    /**
     * 获取默认名称序号
     * @param request
     * @param responseObserver
     */
//    @Override
//    public void getNameSequence(GetNameSequenceRequest request,
//                                StreamObserver<GetNameSequenceResponse> responseObserver) {
//        GetNameSequenceResponse.Builder builder = GetNameSequenceResponse.newBuilder();
//
//        // 组装参数
//        GetNameSequenceParam param = requestToGetNameSequenceParam(request);
//
//        // 参数校验
//        Set<ConstraintViolation<Object>> violations = GRpcUtil.validateParam(param);
//        if (!CollectionUtils.isEmpty(violations)) {
//            ConstraintViolation<Object> violation = violations.iterator().next();
//            builder.setStatus(RpcStatusResult.error(violation.getMessage()));
//            responseObserver.onNext(builder.build());
//            responseObserver.onCompleted();
//            return;
//        }
//
//        // 获取序号
//        ServiceResult<Integer> result = sequenceService.getNameSequence(param);
//        if (result.isSuccess()) {
//            builder.setStatus(RpcStatusResult.success());
//            builder.setSeq(result.getData());
//        } else {
//            builder.setStatus(RpcStatusResult.error(result.getMsg()));
//        }
//        responseObserver.onNext(builder.build());
//        responseObserver.onCompleted();
//    }


    /**
     *  request to 获取名称序号 参数
     * @param request
     * @return
     */
    private GetNameSequenceParam requestToGetNameSequenceParam(GetNameSequenceRequest request) {
        GetNameSequenceParam param = new GetNameSequenceParam();
        // 组织id
        param.setOrgId(request.getOrgId());
        // 联系人类型
        param.setMemberType(request.getMemberTypeValue());
        // 序号名称
        param.setSequenceType(request.getSequenceTypeValue());
        return param;
    }
}
